package com.hsp_java.collection_;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

public class CollectionIterator {
    public static void main(String[] args) {
        Collection col = new ArrayList();
        col.add(new Book("三国演义", "罗贯中", 77.7));
        col.add(new Book("红楼梦", "曹雪芹", 88.8));
        col.add(new Book("三国演义", "罗贯中", 77.7));
        col.add(new Book("三国演义", "罗贯中", 77.7));
        //1.获取col的迭代器
        Iterator iterator = col.iterator();
        //2.用while循环遍历
        while (iterator.hasNext()) {
            Object next = iterator.next();
            System.out.println("obj = " + next);
        }
        //3.推出循环的时候iterator 指向最后的元素
        //4.再次遍历需要重置迭代器
        iterator = col.iterator();
        System.out.println("=======第二次遍历========");

        while (iterator.hasNext()) {
            Object next = iterator.next();
            System.out.println("obj = " + next);
        }

    }
}
class Book {
    private String name;
    private String author;
    private double price;

    public Book(String name, String author, double price) {
        this.name = name;
        this.author = author;
        this.price = price;
    }

    @Override
    public String toString() {
        return "Book{" +
                "name='" + name + '\'' +
                ", author='" + author + '\'' +
                ", price=" + price +
                '}';
    }
}
